package cn.kgc.houseservice.service;


import cn.kgc.bean.System;
import cn.kgc.bean.*;
import cn.kgc.myhousemapper.mapper.*;
import cn.kgc.service.AliyunSms;
import cn.kgc.service.HouseService;
import cn.kgc.service.MailService;
import cn.kgc.utils.CrowdFundingUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Component
public class HouseServiceImpl implements HouseService {

    @Resource
    HouseMapper houseMapper;
    @Resource
    ManageMapper manageMapper;
    @Resource
    SystemMapper systemMapper;
    @Resource
    LiverMapper liverMapper;
    @Resource
    RoomMapper roomMapper;
    @Resource
    MailService mailService;

    @Override
    public PageInfo<House> selectAllHouse(Integer pageNum, Integer pageSize, String frname, String housename, String houseaddr) {
        PageHelper.startPage(pageNum,pageSize);
     /*   List<House> houses;
        if (frname!=null&&frname.equals("")||houseaddr!=null&&houseaddr.equals("")||houseaddr!=null&&houseaddr.equals("")){
            HouseExample houseExample=new HouseExample();
            HouseExample.Criteria criteria=houseExample.createCriteria();
            criteria.andFrnameLike('%'+frname+'%');
            criteria.andHousenameLike('%'+houseaddr+'%');
            criteria.andHouseaddrLike('%'+houseaddr+'%');
            houses=houseMapper.selectByExample(houseExample);
        }else {
            houses=houseMapper.selectByExample(null);
        }*/
        List<House> houses=null;
        if(frname!=null&&frname.equals("")||houseaddr!=null&&houseaddr.equals("")||houseaddr!=null&&houseaddr.equals("")){
            houses=houseMapper.selectList(new QueryWrapper<House>().like("frname",frname).like("houseaddr",houseaddr).like("housename",housename));
        }else{
            houses=houseMapper.selectList(null);
        }

        PageInfo<House> pageInfo=new PageInfo<>(houses);
        return pageInfo;
    }

    @Override
    public Integer addHouse(House house) {
        int i=houseMapper.insert(house);
        return i;
    }

    @Override
    public Integer delHouse(Integer id) {
        int i=houseMapper.deleteById(id);
        return i;
    }

    @Override
    public House selectById(Integer id) {
        House house=houseMapper.selectById(id);
        return house;
    }

    @Override
    public Integer updateHouse(House house) {
        int i=houseMapper.updateById(house);
        return i;
    }

    @Override
    public Manage login(String username, String password) {
     /*   ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Manage> manages = manageMapper.selectByExample(manageExample);*/
     List<Manage> manages=manageMapper.selectList(new QueryWrapper<Manage>().eq("username",username));
        if (manages.size()<=0){
            return null;
        }else{
            Manage manage=manages.get(0);
            if (CrowdFundingUtils.md5(password).equals( manage.getPassword())){
                java.lang.System.out.println("登录成功");
                return manage;
            }else{
                return null;
            }
        }
    }

    @Override
    public int registHouse(House house, Manage manage) {
        /*程序错误返回3 成功返回2 管理员已存在返回0 验证码不正确返回4 不可以存在同名公寓 存在同名公寓返回1  5为没有发送验证码*/
    /*    ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andUsernameEqualTo(manage.getUsername());
        List<Manage> manages = manageMapper.selectByExample(manageExample);*/
    List<Manage> manages=manageMapper.selectList(new QueryWrapper<Manage>().eq("username",manage.getUsername()));
        if (manages.size()>0){
               return 0;
           }else {
               if (true){
                 /*  HouseExample houseExample=new HouseExample();
                   HouseExample.Criteria criteria1 = houseExample.createCriteria();
                   criteria1.andHousenameEqualTo(house.getHousename());
                   List<House> houses = houseMapper.selectByExample(houseExample);*/
                 List<House> houses=houseMapper.selectList(new QueryWrapper<House>().eq("housename",house.getHousename()));
                   if (houses.size()>0){
                       return 1;
                   }else{
                       int insert = houseMapper.insert(house);
                       if (insert>0){
                           manage.setIsz(1);
                           List<House> datahouses = houseMapper.selectList(new QueryWrapper<House>().eq("housename",house.getHousename()));
                           House datahouse=datahouses.get(0);
                           manage.setHouseid(datahouse.getId());
                           manage.setPassword(CrowdFundingUtils.md5(manage.getPassword()));
                           manage.setName(house.getFrname());
                           manage.setMoney(0.0);
                           manage.setState(1);
                           manageMapper.insert(manage);
                           return 2;
                       }else{
                           return 3;
                       }
                   }

               }else{
                   return 4;


               }
           }
        }

    @Override
    public String sendCode(String phone) {
        String code=CrowdFundingUtils.createCode();
        AliyunSms.sendSms(phone,code);
        java.lang.System.out.println("向"+phone+"发送验证码"+code);
        return code;
    }
    @Override
    public Manage selectZmanagerByUsername(String username) {
/*        ManageExample zmanagerExample=new ManageExample();
        ManageExample.Criteria criteria = zmanagerExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Manage> zmanagers = manageMapper.selectByExample(zmanagerExample);*/
        List<Manage> zmanagers=manageMapper.selectList(new QueryWrapper<Manage>().eq("username",username));
        if ( zmanagers.size()>0){
            return  zmanagers.get(0);
        }else{
            return null;
        }

    }

    @Override
    public int addZmanager(Manage zmanager) {
        return manageMapper.insert(zmanager);
    }

    @Override
    public int updateZmanager(Manage zmanager) {
        return manageMapper.updateById(zmanager);
    }

    @Override
    public System selectSystem() {
        List<cn.kgc.bean.System> systems = systemMapper.selectList(null);
        if (systems.size()>0){
            return systems.get(0);
        }else{
            return null;
        }
    }

    @Override
    /*0 用户不存在 1为用户不属于该公寓 2为成功 3为系统错误 4为房间不属于公寓 5为房间不存在 6为房间人数已满*/
    public int add1(Integer liverid, Integer roomid,Integer houseid) {
        Liver liver = liverMapper.selectById(liverid);
        if (liver==null){
            return 0;
        }else{
            if (liver.getHouseid()!=houseid){
                java.lang.System.out.println(liver.getHouseid());
                java.lang.System.out.println(houseid);
                return 1;
            }else{
                Room room = roomMapper.selectById(roomid);
                if (room!=null){
                    if (!room.getHouseid().equals(houseid)){
                        return 4;
                    }else{
                        if (room.getRoomnow()>=room.getRoomsize()){
                            return 6;
                        }else{
                            Integer roomid1 = liver.getRoomid();
                            if (roomid1!=null){
                                Room room1 = roomMapper.selectById(roomid);
                                room1.setRoomnow(room1.getRoomnow()-1);
                                roomMapper.updateById(room1);
                            }
                            liver.setRoomid(roomid);
                            int i = liverMapper.updateById(liver);
                            room.setRoomnow(room.getRoomnow()+1);
                            int i1 = roomMapper.updateById(room);
                            if (i>0&&i1>0){
                                return 2;
                            }else{
                                return 3;
                            }
                        }

                    }


                }else{
                    return 5;
                }
            }
        }

    }

    @Override
    /*返回:0为手机号已注册 1为房间不存在 3为房间不属于本公寓 4为房间人数已满 2为成功 5为系统错误*/
    public int add2(Liver liver) {
/*        LiverExample liverExample=new LiverExample();
        LiverExample.Criteria criteria = liverExample.createCriteria();
        criteria.andPhoneEqualTo(liver.getPhone());
        List<Liver> livers = liverMapper.selectByExample(liverExample);*/
        List<Liver> livers =liverMapper.selectList(new QueryWrapper<Liver>().eq("phone",liver.getPhone()));
        if (livers.size()>0){
            return 0;
        }else{
/*            RoomExample roomExample=new RoomExample();
            RoomExample.Criteria roomExampleCriteria = roomExample.createCriteria();
            roomExampleCriteria.andIdEqualTo(liver.getRoomid());
            List<Room> rooms = roomMapper.selectByExample(roomExample);*/
            List<Room> rooms =roomMapper.selectList(new QueryWrapper<Room>().eq("id",liver.getRoomid()));
            if (rooms.size()<=0){
                return 1;
            }else{
                Room roomdata=rooms.get(0);
                if (roomdata.getHouseid()!=liver.getHouseid()){
                    return 3;
                }else{
                   if (roomdata.getRoomnow()>=roomdata.getRoomsize()){
                       return 4;
                   }else{
                       liver.setPassword(CrowdFundingUtils.md5(liver.getPassword()));
                       liver.setMoney(0.0);
                       liver.setEndtime(new Date());
                       liver.setStarttime(new Date());
                       int insert = liverMapper.insert(liver);
                       if (insert>0){
                           return 2;
                       }else{
                           return 5;
                       }
                   }
                }
            }

        }
    }

    @Override
    /*返回0 用户名已存在  1公寓不存在 2为成功  3为系统错误*/
    public int houseregist(Manage manage) {
        String username = manage.getUsername();
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andUsernameEqualTo(username);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("username",username));
        if (manages.size()>0){
            return 0;
        }else{
            Integer houseid = manage.getHouseid();
            House house = houseMapper.selectById(houseid);
            if (house==null){
                return 1;
            }else{
                manage.setState(0);
                manage.setMoney(0.0);
                manage.setIsz(0);
                manage.setPassword(CrowdFundingUtils.md5(manage.getPassword()));
                int insert = manageMapper.insert(manage);
                if (insert>0){
                    return 2;
                }else{
                    return 3;
                }
            }

        }

    }

    @Override
    public String sendEmail(String email) {
        String code=CrowdFundingUtils.createCode();
        mailService.sendSimpleMail(email,"MyHouse公寓管理系统-公寓管理员邮箱绑定验证","您好,尊敬的MyHouse公寓管理系统-公寓管理员:\n您于"+new Date()+"于MyHouse系统提交了邮箱绑定请求,\n为您生成的验证码为:["+code+"]\n为了您的帐号安全,请不要泄露他人!\n祝您生活愉快,工作顺利!\nFrom:MyHouse制作团队");
        java.lang.System.out.println("向"+email+"发送验证码"+code);
        return code;
    }
    @Override
    /*返回0 邮箱已被绑定 1系统错误 2=成功*/
    public int bindEmail(String email, Integer id) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andEmailEqualTo(email);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("email",email));
        if (manages.size()>0){
            return 0;
        }else{
            Manage manage = manageMapper.selectById(id);
            manage.setEmail(email);
            int i = manageMapper.updateById(manage);
            if (i>0){
                return 2;
            }else{
                return 1;
            }
        }

    }

    @Override
    /*0 邮箱未绑定管理员 2成功*/
    public int forget(String email) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andEmailEqualTo(email);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("email",email));
        if (manages.size()<=0){
            return 0;
        }else{
            Manage manage=manages.get(0);
            manage.setCode(CrowdFundingUtils.md5(CrowdFundingUtils.createCode()));
            manageMapper.updateById(manage);
            mailService.sendSimpleMail(email,"MyHouse密码找回邮件","尊敬的MyHouse用户:\n 您于"+new Date()+"在MyHouse系统进行了密码找回操作\n由于系统数据进行加密,所以您的密码无法直接找回,您可以通过以下链接进行密码重置:\n" +
                    "http://localhost:8081/house/manage/passwordset?email="+email+"&code="+manage.getCode()+"\n如果邮箱内无法直接访问,请复制链接到浏览器进行操作\n[注:链接打开一次后将会失效,请谨慎操作!]\n如果以上方法无法解决您的问题,您可以尝试发送邮件至feip966@126.com与我们反馈\n祝您生活愉快,工作顺利\nFrom:MyHouse开发团队");
            return 2;

        }
    }

    @Override
    public Manage getManageByEmail(String email) {
/*        ManageExample manageExample=new ManageExample();
        ManageExample.Criteria criteria = manageExample.createCriteria();
        criteria.andEmailEqualTo(email);*/
        List<Manage> manages = manageMapper.selectList(new QueryWrapper<Manage>().eq("email",email));
        if (manages.size()>0){
            return manages.get(0);
        }else{
            return null;
        }
    }

    @Override
    public void reset(String newpassword, Integer id) {
        Manage manage = manageMapper.selectById(id);
        manage.setPassword(CrowdFundingUtils.md5(newpassword));
        manageMapper.updateById(manage);
    }

    @Override
    public PageInfo<Liver> getLivers(Integer pageNum,Integer pageSize,Integer houseid,String livername,Integer roomid) {
        PageHelper.startPage(pageNum,pageSize);
/*        LiverExample liverExample=new LiverExample();
        LiverExample.Criteria criteria = liverExample.createCriteria();
        criteria.andHouseidEqualTo(houseid);
        if (livername!=null&&!livername.equals("")){
            criteria.andNameLike('%'+livername+'%');
        }
        if (roomid!=null&&roomid!=0){
            criteria.andRoomidEqualTo(roomid);
        }*/
        QueryWrapper<Liver> queryWrapper=new QueryWrapper<Liver>();
        if (livername!=null&&!livername.equals("")){
            queryWrapper.eq("name",livername);
        }
        if (roomid!=null&&roomid!=0){
            queryWrapper.eq("roomid",roomid);
        }
        List<Liver> livers = liverMapper.selectList(queryWrapper);
        PageInfo<Liver> pageInfo=new PageInfo<>(livers);
        return pageInfo;
    }

    @Override
    public int addroom(Room room) {
        room.setRoomnow(0);
        return roomMapper.insert(room);
    }

}
